﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Mystery.SharePoint
{
    public static class SPListCollectionExtensions
    {
        /// <summary>
        /// Provisions a list if it doesn't already exist
        /// </summary>
        /// <param name="lists"></param>
        /// <param name="title">Title of list, may be a resource expression</param>
        /// <param name="description">Description of list, may be a resource expression</param>
        /// <param name="url">Url of list, may be a resource expression</param>
        /// <param name="featureId">Feature implementing the list type</param>
        /// <param name="templateType">Type of template</param>
        /// <param name="docTemplateType">Type of documents to include</param>
        /// <param name="quickLaunchOptions">Options for the quick launch</param>
        /// <returns></returns>
        public static SPList Provision(this SPListCollection lists, string title, string description, string url, string featureId, SPListTemplateType templateType, string docTemplateType, SPListTemplate.QuickLaunchOptions quickLaunchOptions)
        {
            uint lcid = (uint)System.Threading.Thread.CurrentThread.CurrentUICulture.LCID;
            string defaultResource = "core";

            SPList l = lists.TryGetList(SPUtility.GetLocalizedString(title,defaultResource,lcid));
            if (l != null) return l;

            Guid id = lists.Add(
                SPUtility.GetLocalizedString(title, defaultResource, lcid),
                SPUtility.GetLocalizedString(description, defaultResource, lcid),
                SPUtility.GetLocalizedString(url, defaultResource, lcid), 
                featureId, 
                (int)templateType, 
                docTemplateType, 
                quickLaunchOptions);

            return lists[id];
        }
    }
}
